package com.shujia.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Properties;

public class Demo2Cars {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092");

        //指定数据的格式
        properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        //创建生产者
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        //读取数据
        FileReader fileReader = new FileReader("kafka/data/cars.json");
        //转换成缓冲流
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        //循环读取数据
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            //生产数据
            kafkaProducer.send(new ProducerRecord<>("cars", line));
            kafkaProducer.flush();
            //停一会
            Thread.sleep(5);
        }

        kafkaProducer.close();

    }
}
